package com.hetao.xx.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.constants;
import com.hetao.xx.game.model.Place;
import com.hetao.xx.game.model.User;
import com.hetao.xx.orm.NpcEventVo;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.orm.PlaceVo;
import com.hetao.xx.service.PlaceManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeFragment extends Fragment {

    private View root;
    private Unbinder unbinder;

    @BindView(R.id.home_city)
    TextView cityName;

    private final Global global = Global.getInstant();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_main_home, container, false);
            unbinder = ButterKnife.bind(this, root);
            init();
        }

        return root;
    }

    public void init() {
        User user = global.getUser();
        PlaceVo curPlace = global.placeManager.getById(user.info.getPlaceId());
        if (curPlace != null) {
            switchFragment(constants.PlacePage, 0, curPlace);
        }
    }


    public void switchFragment(int arg1, int arg2, Object object) {
        Fragment fragment = null;
        switch (arg1) {
            case constants.DialogPage:
//                NpcEvent event = (NpcEvent) object;
//                HomeDialogFragment dialogFragment = new HomeDialogFragment();
//                dialogFragment.setEvent(event);
//                fragment = dialogFragment;
                break;
            case constants.PlacePage:
                PlaceVo curPlace = null;
                if (object != null) {
                    curPlace = (PlaceVo)object;
                } else {
                    curPlace = Global.getInstant().placeManager.getById(arg2);
                }

                if(curPlace != null) {
                    cityName.setText(curPlace.name);

                    HomePlaceFragment cityFragment = new HomePlaceFragment();
                    cityFragment.setPlaces(curPlace);
                    fragment = cityFragment;
                }
                break;
            case constants.NpcPage:
                NpcVo npc = null;
                if (object != null) {
                    npc = (NpcVo)object;
                } else {
                    npc = Global.getInstant().placeManager.getNpcById(arg2);
                }

                if(npc != null) {
                    HomeNpcFragment npcFragment = new HomeNpcFragment();
                    npcFragment.setNpc(npc);
                    fragment = npcFragment;
                }
                break;
            case constants.EventPage:
                NpcEventVo event = null;
                if (object != null) {
                    event = (NpcEventVo)object;
                } else {
                    event = Global.getInstant().placeManager.getEventById(arg2);
                }

                if(event != null) {
                    HomeEventFragment eventFragment = new HomeEventFragment();
                    eventFragment.setEvent(event);
                    fragment = eventFragment;
                }
                break;
        }

        if (fragment != null) {
            FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
            transaction.replace(R.id.main_frame_layout, fragment);
            transaction.commit();
        }
    }


    public void updateSite(String site) {
        cityName.setText(site);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
